package edu.gtcfla.km.appmodules.service.impl;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.vsg.biz.BusinessOperationException;
import org.vsg.biz.account.User;
import org.vsg.biz.account.repositories.UserRepo;
import org.vsg.common.page.AbstractPage;
import org.vsg.common.page.PageContentConverter;
import org.vsg.common.page.PageConverterTemplate;
import org.vsg.search.api.criterion.PaginationCriteria;
import org.vsg.search.api.page.PaginationException;

import edu.gtcfla.km.Logs;
import edu.gtcfla.km.appmodules.service.LogsService;
import edu.gtcfla.km.appmodules.vo.LogsFormVO;
import edu.gtcfla.km.appmodules.vo.LogsListVO;
import edu.gtcfla.km.appmodules.vo.LogsMemberFormVO;
import edu.gtcfla.km.repositories.LogsRepo;

/**
 * @author Bill Vison
 *
 */
public class LogsServiceImpl implements LogsService {

	@Autowired(required=false)
	private LogsRepo<Logs, String> logsRepo;	
	
	@Autowired(required=false)	
	private UserRepo<User,String> userRepo;
	
	/* (non-Javadoc)
	 * @see edu.gtcfla.km.appmodules.service.LogsService#addLogs(edu.gtcfla.km.appmodules.vo.LogsFormVO)
	 */
	@Override
	public void addLogs(LogsFormVO formVO)
			throws BusinessOperationException {
		// TODO Auto-generated method stub
		
		Logs ug = formVO.getEntity();
		
		logsRepo.save( ug );

	}

	/* (non-Javadoc)
	 * @see edu.gtcfla.km.appmodules.service.LogsService#updateLogs(edu.gtcfla.km.appmodules.vo.LogsFormVO)
	 */
	@Override
	public void updateLogs(LogsFormVO formVO)
			throws BusinessOperationException {
		// TODO Auto-generated method stub
		
		Logs inputEntity = logsRepo.findOne( formVO.getId() );
		
		if ( formVO.getName() != null ) {
			if ( !inputEntity.getName().equals( formVO.getName() )) {
				inputEntity.setName( formVO.getName() );
			}
		}
		
		if ( formVO.getDate() != null ) {
			if ( !inputEntity.getDate().equals( formVO.getDate() )) {
				inputEntity.setDate( formVO.getDate() );
			}
		}
		
		logsRepo.update( inputEntity );

	}

	/* (non-Javadoc)
	 * @see edu.gtcfla.km.appmodules.service.LogsService#loadLogs(java.lang.String)
	 */
	@Override
	public LogsFormVO loadLogs(String pk)
			throws BusinessOperationException {
		// TODO Auto-generated method stub
		Logs inputEntity = logsRepo.findOne( pk );
		
		LogsFormVO ugf = new LogsFormVO( inputEntity );
		
		return ugf;
	}

	/* (non-Javadoc)
	 * @see edu.gtcfla.km.appmodules.service.LogsService#deleteLogsIds(java.lang.String[])
	 */
	@Override
	public void deleteLogsIds(String[] ids)
			throws BusinessOperationException {
		// TODO Auto-generated method stub
		
		Set<String> idSet = new LinkedHashSet<String>();
		
		for (String id : ids) {
			idSet.add( id );
		}
		
		Map<String, Logs> LogsMap = logsRepo.findAllByIds( idSet ); 
		
		for (Logs item : LogsMap.values()) {
			logsRepo.deleteFully( item );
		}
	

	}

	/* (non-Javadoc)
	 * @see edu.gtcfla.km.appmodules.service.LogsService#pageAvailableAllList(org.vsg.search.api.criterion.PaginationCriteria)
	 */
	@Override
	public Page<LogsListVO> pageAvailableAllList(
			PaginationCriteria criteria) throws BusinessOperationException,
			PaginationException {
		// TODO Auto-generated method stub
		
		Page<Logs> pageEnties =  logsRepo.pageAviableLogs( criteria );
		
		PageContentConverter<LogsListVO , Logs> converter = new PageContentConverter<LogsListVO , Logs>() {

			@Override
			public LogsListVO convert(Logs input) {
				LogsListVO item = new LogsListVO(input);
				
				return item;
			}
		};		

		PageConverterTemplate<LogsListVO , Logs> pageTemplate = 
				new PageConverterTemplate<LogsListVO , Logs>( pageEnties );		
		
		Page<LogsListVO> target = pageTemplate.convertContent(converter);
		AbstractPage cpi = (AbstractPage)target;
		cpi.setTotalRecord( (int) pageEnties.getTotalElements());
		
		
		return target;
	}

	/* (non-Javadoc)
	 * @see edu.gtcfla.km.appmodules.service.LogsService#assignMember(java.util.Collection, java.lang.String)
	 */
	@Override
	public void assignMember(Collection<String> userIds, String LogsId)
			throws BusinessOperationException {
		// TODO Auto-generated method stub

		
		//Map<String, User> usersMap = userRepo.findAllPOByIds( new LinkedHashSet<String>(userIds) );
		
		
		logsRepo.assignMembersToLogs(new LinkedHashSet<String>( userIds ), LogsId);
		
		/*
		Set<User>  members = Logs.getMembers();

		System.out.println("first : " + members);		
		
		// --- remove old memeber ---
	
		Set<String> orgMemIds = new LinkedHashSet<String>();
		for (Iterator<User> orgMemIter = members.iterator() ; orgMemIter.hasNext() ; ) {
			User org = orgMemIter.next();
			orgMemIds.add( org.getId() );
			
			System.out.println("user id : " +  org.getId() + " , " + ((UserD)org).getAssignedRolesSet()  );
			
			// --- remove old object ---
			if ( !userIds.contains(org.getId() ) ) {
				//orgMemIter.remove();
			}
		}
		
		System.out.println("org : " + members);

		
		for (User user : usersMap.values() ) {

			System.out.println("new user id : " +  user.getId() + " , " + ((UserD)user).getAssignedRolesSet()  );
						
			
			// --- the same object ,not change --
			if (orgMemIds.contains( user.getId() ) ) {
				continue;
			}

			
			System.out.println(user.getId() + " , " + user + " , " + usersMap.get(user.getId() ) + " , " );
			members.add(user);

		}

		
		//Logs.setMembers( members );

		//logsRepo.update( Logs );
		*/
	}

	@Override
	public LogsMemberFormVO getAssignedMembersByLogsId(String LogsId)
			throws BusinessOperationException {
		
		/*Logs Logs = logsRepo.loadOne( LogsId );

		Set<User> members =  Logs.getMembers();
		
		Set<String> memberIds = new LinkedHashSet<String>();
		
		if (!members.isEmpty()) {
			for (User member : members) {
				memberIds.add( member.getId() );
			}
		}
		
		LogsMemberFormVO memberForm = new LogsMemberFormVO(Logs);
		memberForm.setMembers( memberIds );
		return memberForm;*/
		return null;
	}
	


}
